package com.dwin.dingnuo.ui.system;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dwin.dingnuo.R;
import com.dwin.dingnuo.http.DwinConstants;
import com.dwin.dingnuo.http.MyRetrofit;
import com.dwin.dingnuo.http.beans.ResponseTokenVerify;
import com.dwin.dingnuo.http.beans.UserBean;
import com.dwin.dingnuo.http.request.LoginBean;
import com.dwin.dingnuo.ui.MainActivity;
import com.dwin.dingnuo.ui.base.BaseActivity;
import com.dwin.dingnuo.utils.ActivityUtils;
import com.dwin.dingnuo.utils.ToastUtil;
import com.dwin.dingnuo.views.NetLoadingDialog;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;
import retrofit2.Response;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity implements TextView.OnEditorActionListener {
    private static final String TAG = "LoginActivity";
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.et_account)
    EditText et_account;
    @BindView(R.id.btn_login)
    Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        et_password.setOnEditorActionListener(this);
    }

    // 登录操作
    @OnClick(R.id.btn_login)
    void goLogin() {
        String account = et_account.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtil.toastLong(mContext, "请输入账号");
            et_account.requestFocus();
            return;
        } else if (TextUtils.isEmpty(password)) {
            ToastUtil.toastLong(mContext, "请输入密码");
            et_password.requestFocus();
            return;
        }
        if (isNetworkConnected(this)) {
            clientLogin(account, password);  // 密码登录
        } else {
            ToastUtil.toastShort(this, R.string.net_error);
        }
    }

    private void clientLogin(final String phone, final String pwd) {
        NetLoadingDialog.showprogress(mContext, true);
        LoginBean info = new LoginBean(phone, pwd);
        Gson gson = new Gson();
        String obj = gson.toJson(info);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        MyRetrofit.getInstance().clientLogin(language, body, new retrofit2.Callback<ResponseTokenVerify>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseTokenVerify> call, Response<ResponseTokenVerify> response) {
                if (DwinConstants.HTTP_OK == response.body().code) {
                    UserBean user = response.body().data;
                    LitePal.deleteAll(UserBean.class); // 先删除所有数据
                    user.save();
                    findAllData();
                    ActivityUtils.switchActivityFinish(mContext, MainActivity.class);
                } else if (DwinConstants.HTTP_TOKEN_100002 == response.body().code) {
                    userLogout();
                } else {
                    ToastUtil.toastShort(mContext, response.body().message);
                }
                NetLoadingDialog.dismissprogress();
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseTokenVerify> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                NetLoadingDialog.dismissprogress();
            }
        });
    }

    private void findAllData() {
        List<UserBean> list = LitePal.findAll(UserBean.class);
        Log.d(TAG, "查询结果--------start------： ");
        for (UserBean userBean : list) {
            Log.d(TAG, "查询结果： " + userBean.toString());
        }
        Log.d(TAG, "查询结果--------end------： ");
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        switch (actionId) {
            case EditorInfo.IME_ACTION_DONE:
                btn_login.performClick();
                break;
        }
        return false;
    }

}
